{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d35b2266",
   "metadata": {},
   "source": [
    "# Loop statement"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8b244a10",
   "metadata": {},
   "source": [
    "## Built-in functions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "289b239d",
   "metadata": {},
   "outputs": [],
   "source": [
    "# range()函数\n",
    "# 用于生成一个整数序列\n",
    "# 创建range对象的三种方式\n",
    "\n",
    "#range() function\n",
    "\n",
    "#Used to generate a sequence of integers\n",
    "\n",
    "#Three ways to create a range object\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3cc82bd3",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(0, 10)\n",
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "# range()三种创建方式\n",
    "#第一种创建方式，只有一个参数(小括号中只给了一个数)\n",
    "\n",
    "r=range(10) #stop 默认从0开始，步长为1，所以是 0，1，2，3，4。。。\n",
    "print(r) # range(0,10)\n",
    "print(list(r)) #用于查看range对象中的整数序列\n",
    "# list 是列表的意思\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "677bfc61",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
     ]
    }
   ],
   "source": [
    "# 第二种方法，给了两个参数(小括号中给了两个数)\n",
    "r=range(1,10)#指定了起始值，从1开始到10结束，(不包含10)，默认步长为1.\n",
    "print(list(r))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "caca74d5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 3, 5, 7, 9]\n"
     ]
    }
   ],
   "source": [
    "# 第三种方法，给了三个参数，（小括号中给了三个数）\n",
    "r=range(1,10,2)\n",
    "print(list(r))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "36260f56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "True\n"
     ]
    }
   ],
   "source": [
    " # 判断指定的整数 在序列中是否存在 in,not in\n",
    "print(10 in r) #False ，10不在当前的r这个整数序列中\n",
    "print(9 in r) #True ，9在当关的r这个序列中"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "91b3ee3d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "False\n"
     ]
    }
   ],
   "source": [
    "print(10 not in r)\n",
    "print(9 not in r)\n",
    "#in and not in 判断整数序列中是否存在(不存在)指定的整数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "4da4e2f5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# range 类型的优点:\n",
    "    #不管range对象表示的整数序列有多长，所有range对象占用的内存空间都是相同的，\n",
    "    #因为仅仅需要存储 start和 stop和step，只有当用到对象时，才会去计算序列中的相关元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "73858995",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "range(1, 20)\n",
      "range(1, 101)\n"
     ]
    }
   ],
   "source": [
    "print(range(1,20,1))# 1-19\n",
    "print(range(1,101,1))#从1开始到100结束的"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9eb3f7e0",
   "metadata": {},
   "source": [
    "## while loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "490b7172",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 选择结构的if与循环结构while的区别\n",
    "# if是判断一次，条件为True执行一行\n",
    "# while是判断N+1次，条件为True执行N次\n",
    "# 为False的话就会退出了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "3fc54b86",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "a=1 #判断条件表达式\n",
    "\n",
    "while a<10:#执行条件体\n",
    "    print(a)\n",
    "    a+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "56744c30",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "# 和if的区别\n",
    "a=1\n",
    "if a<10:\n",
    "    print(a)\n",
    "    a+=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "43b8608c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# if 与while循环的区别，\n",
    "# if是判断一次，条件表达式为True就结束了\n",
    "# while是判断 N+1次，条件为True执行N次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "ddcd2f10",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "和为: 10\n"
     ]
    }
   ],
   "source": [
    "# 计算0到4之间的累加和\n",
    "# 1.初始化变量\n",
    "# 2.条件判断\n",
    "# 3.条件执行体\n",
    "# 4.改变变量\n",
    "# 总结：初始化的变量与条件判断的变量与改变的变量为同一个\n",
    "sum=0\n",
    "a=0\n",
    "\n",
    "# 2.条件判断\n",
    "while a < 5:\n",
    "    #条件执行体(循环体)\n",
    "    sum+=a\n",
    "    \n",
    "    #改变变量\n",
    "    a+=1\n",
    "print(\"和为:\",sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "4837d1b5",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n",
      "1-100之间的偶数和: 2500\n"
     ]
    }
   ],
   "source": [
    "# Test 1-100之间的偶数\n",
    "print(bool(0))\n",
    "sum=0\n",
    "# 初始化变量\n",
    "a=1\n",
    "\n",
    "# 条件判断 \n",
    "while a<=100:\n",
    "    #条件执行(求和)\n",
    "    #条件判断是否为偶数\n",
    "    # if a%2 ==0:这个代码可以改成\n",
    "    if bool(a%2):\n",
    "    #0的布尔值为False\n",
    "    # 改下条件判断偶数和就变成了奇数和\n",
    "        sum+=a\n",
    "        \n",
    "        #改变变量\n",
    "    a+=1\n",
    "print(\"1-100之间的偶数和:\",sum)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cab6885c",
   "metadata": {},
   "source": [
    "## for-in循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3c00b131",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "9d2da103",
   "metadata": {},
   "outputs": [],
   "source": [
    "#for in 循环，in表达从(字符串，序列等)中依次去值,又称为遍历\n",
    "#遍历的对象必须是可迭代对象\n",
    "# for自定义的变量 in 可迭代对象：\n",
    "# 循环体"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f503a97f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "p\n",
      "y\n",
      "t\n",
      "h\n",
      "o\n",
      "n\n"
     ]
    }
   ],
   "source": [
    "for item in'python':\n",
    "    print(item)\n",
    "    #第一次取出来的是p，将p赋值值item，将item的值输出\n",
    "    \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "5b648a75",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n"
     ]
    }
   ],
   "source": [
    "for i in range(10):#range()能产生一个整数序列，也是一个可迭代对象\n",
    "    print(i)\n",
    "    #执行了10次"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "4a24e91d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "winner!\n",
      "winner!\n",
      "winner!\n",
      "winner!\n",
      "winner!\n"
     ]
    }
   ],
   "source": [
    "# 如果在循环体中不需要使用到自定义变量，可将自定义变量写为\"_\"\n",
    "for _ in range(5):\n",
    "    print(\"winner!\")\n",
    "    #一定记住空格缩紧！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "23c0c211",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "和 2\n",
      "和 6\n",
      "和 12\n",
      "和 20\n",
      "和 30\n",
      "和 42\n",
      "和 56\n",
      "和 72\n",
      "和 90\n",
      "和 110\n",
      "和 132\n",
      "和 156\n",
      "和 182\n",
      "和 210\n",
      "和 240\n",
      "和 272\n",
      "和 306\n",
      "和 342\n",
      "和 380\n",
      "和 420\n",
      "和 462\n",
      "和 506\n",
      "和 552\n",
      "和 600\n",
      "和 650\n",
      "和 702\n",
      "和 756\n",
      "和 812\n",
      "和 870\n",
      "和 930\n",
      "和 992\n",
      "和 1056\n",
      "和 1122\n",
      "和 1190\n",
      "和 1260\n",
      "和 1332\n",
      "和 1406\n",
      "和 1482\n",
      "和 1560\n",
      "和 1640\n",
      "和 1722\n",
      "和 1806\n",
      "和 1892\n",
      "和 1980\n",
      "和 2070\n",
      "和 2162\n",
      "和 2256\n",
      "和 2352\n",
      "和 2450\n",
      "和 2550\n"
     ]
    }
   ],
   "source": [
    "# Test 使用for循环，计算1到100之间的偶数和\n",
    "\n",
    "sum=0 #用于存储偶数和\n",
    "for item in range(1,101):\n",
    "    if item %2==0:\n",
    "        sum+=item\n",
    "        \n",
    "        print(\"和\",sum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "393d23a6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "153\n",
      "370\n",
      "371\n",
      "407\n"
     ]
    }
   ],
   "source": [
    "# Test 输出100到999之间的水仙花数\n",
    "# 举例 153=3*3*3+5*5*5+1*1*1\n",
    "for item in range(100,1000):\n",
    "    ge=item%10 #个位\n",
    "    shi=item//10%10#十位\n",
    "    bai=item//100 #百位\n",
    "    #print(ge,shi,bai)\n",
    "    \n",
    "    #判断\n",
    "    if ge**3+shi**3+bai**3==item:\n",
    "        print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "d4e07849",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入密码:1231\n",
      "密码不正确\n",
      "请输入密码:12412\n",
      "密码不正确\n",
      "请输入密码:888\n",
      "bingo\n"
     ]
    }
   ],
   "source": [
    "# 从键盘输入密码，最多登陆三次，如果正确就结束循环\n",
    "for item in range(3):\n",
    "    pwd=input(\"请输入密码:\")\n",
    "    if pwd=='888':\n",
    "        print(\"bingo\")\n",
    "        break\n",
    "    else:\n",
    "        print(\"密码不正确\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "b4d4b839",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入密码:3525\n",
      "密码不正确\n",
      "请输入密码:32523\n",
      "密码不正确\n",
      "请输入密码:235\n",
      "密码不正确\n"
     ]
    }
   ],
   "source": [
    "a=0\n",
    "while a<3:\n",
    "    pwd=input(\"请输入密码:\")\n",
    "    if pwd=='888':\n",
    "        print(\"密码正确\")\n",
    "        break\n",
    "    else:\n",
    "        print(\"密码不正确\")\n",
    "        \n",
    "        #改变变量\n",
    "        a+=1\n",
    "        #这样的话，不正确三次就自动结束了。\n",
    "        #也可以用while循环，也可以用for in循环。\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ff7e6ee8",
   "metadata": {},
   "source": [
    "## continue "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "8faf5872",
   "metadata": {},
   "outputs": [],
   "source": [
    "#用于结束当前循环，进入下一次循环，通常与分支结构汇总的if一起使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1a34ab28",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "10\n",
      "15\n",
      "20\n",
      "25\n",
      "30\n",
      "35\n",
      "40\n",
      "45\n",
      "50\n"
     ]
    }
   ],
   "source": [
    "# 要求输出1到50之间所有5的倍数，5，10，15，20，25.\n",
    "# 5的倍数的共同点，和5的余数为0的数都是5的倍数\n",
    "# 什么样的数不是5的倍数，1，2，3，4，5，6，7，8，9 与5的余数不为0的数都不是5的倍数\n",
    "for item in range(1,51):\n",
    "    if item%5==0:\n",
    "        print(item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "a9bd6729",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 但是要求是用 continue实现"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "9346dafd",
   "metadata": {},
   "outputs": [],
   "source": [
    "for item in range(1,51):\n",
    "    if item%5!=0:\n",
    "        \n",
    "        continue\n",
    "        \n",
    "        print(item)\n",
    "        "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84ab79f4",
   "metadata": {},
   "source": [
    "## else"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b50eab4a",
   "metadata": {},
   "outputs": [],
   "source": [
    "# else语句 和else语句配合使用的三种情况\n",
    "# if else  if条件表达式不成立时执行else\n",
    "# while else 没有碰到break时执行else\n",
    "# for else 同上"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "32fc0385",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入密码:3132\n",
      "密码不正确\n",
      "请输入密码:23123\n",
      "密码不正确\n",
      "请输入密码:414\n",
      "密码不正确\n",
      "对不起，密码三次输入都错误\n"
     ]
    }
   ],
   "source": [
    "for item in range(3):\n",
    "    pwd=input(\"请输入密码:\")\n",
    "    if pwd=='888':\n",
    "        print(\"密码正确\")\n",
    "        break\n",
    "    else:\n",
    "        print(\"密码不正确\")\n",
    "else: #这个else 是和 for 对齐的\n",
    "    print(\"对不起，密码三次输入都错误\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6a16927f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入密码231312\n",
      "密码不正确\n",
      "请输入密码141\n",
      "密码不正确\n",
      "请输入密码1424\n",
      "密码不正确\n",
      "三次密码均输入错误\n"
     ]
    }
   ],
   "source": [
    "#也可以用 while 循环这样写\n",
    "a=0\n",
    "while a<3:\n",
    "    pwd=input(\"请输入密码\")\n",
    "    if pwd=='888':\n",
    "        print(\"密码正确\")\n",
    "        break\n",
    "    else:\n",
    "        print(\"密码不正确\")\n",
    "        a+=1\n",
    "        \n",
    "else:\n",
    "    print('三次密码均输入错误')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bac36601",
   "metadata": {},
   "source": [
    "## 중첩 루프"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "423d6177",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*\t*\t*\t*\t\n",
      "*\t*\t*\t*\t\n",
      "*\t*\t*\t*\t\n"
     ]
    }
   ],
   "source": [
    "# 嵌套循环\n",
    "# 输入一个三行四列的矩形\n",
    "for i in range(1,4):#外行表执行三次，一次是一行\n",
    "    for j in range(1,5):#内层控制的打印个数\n",
    "        print('*',end='\\t')#不换行输出\n",
    "    print()#打行\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "a2643ad4",
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "*\n",
      "**\n",
      "***\n",
      "****\n",
      "*****\n",
      "******\n",
      "*******\n",
      "********\n",
      "*********\n"
     ]
    }
   ],
   "source": [
    "# Test \n",
    "for i in range(1,10):\n",
    "    # 找行和列的关系\n",
    "    for j in range(1,i+1):\n",
    "        print('*',end='')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "655a7758",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 * 1 = 1\t\n",
      "2 * 1 = 2\t2 * 2 = 4\t\n",
      "3 * 1 = 3\t3 * 2 = 6\t3 * 3 = 9\t\n",
      "4 * 1 = 4\t4 * 2 = 8\t4 * 3 = 12\t4 * 4 = 16\t\n",
      "5 * 1 = 5\t5 * 2 = 10\t5 * 3 = 15\t5 * 4 = 20\t5 * 5 = 25\t\n",
      "6 * 1 = 6\t6 * 2 = 12\t6 * 3 = 18\t6 * 4 = 24\t6 * 5 = 30\t6 * 6 = 36\t\n",
      "7 * 1 = 7\t7 * 2 = 14\t7 * 3 = 21\t7 * 4 = 28\t7 * 5 = 35\t7 * 6 = 42\t7 * 7 = 49\t\n",
      "8 * 1 = 8\t8 * 2 = 16\t8 * 3 = 24\t8 * 4 = 32\t8 * 5 = 40\t8 * 6 = 48\t8 * 7 = 56\t8 * 8 = 64\t\n",
      "9 * 1 = 9\t9 * 2 = 18\t9 * 3 = 27\t9 * 4 = 36\t9 * 5 = 45\t9 * 6 = 54\t9 * 7 = 63\t9 * 8 = 72\t9 * 9 = 81\t\n"
     ]
    }
   ],
   "source": [
    "for i in range(1,10):\n",
    "    for j in range(1,i+1):\n",
    "        print(i,'*',j,'=',i*j,end='\\t')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4bb5d4aa",
   "metadata": {},
   "source": [
    "## 二重循环中的break和continue"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "4cf7435c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "1\n",
      "1\n",
      "1\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "# break and continue\n",
    "# 流程控制语句在二重循环中的使用\n",
    "for i in range(5): #代表外层循环要执行五次\n",
    "    for j in range(1,11):\n",
    "        if j%2==0:\n",
    "            break\n",
    "            # 里面这个循环没用\n",
    "        print(j)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "f1f91f23",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\t3\t5\t7\t9\t\n",
      "1\t3\t5\t7\t9\t\n",
      "1\t3\t5\t7\t9\t\n",
      "1\t3\t5\t7\t9\t\n",
      "1\t3\t5\t7\t9\t\n"
     ]
    }
   ],
   "source": [
    "# continue\n",
    "for i in range(5): #代表外层循环要执行五次\n",
    "    for j in range(1,11):\n",
    "        if j%2==0:\n",
    "            continue\n",
    "         \n",
    "        print(j,end='\\t')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2a06d45",
   "metadata": {},
   "outputs": [],
   "source": [
    "# End"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
